<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>保养记录</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="/static/layui/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="/static/layui/css/public.css" media="all" />
</head>
<body class="childrenBody">
<form class="layui-form">
    <blockquote class="layui-elem-quote quoteBox">
        <form class="layui-form">
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <select id="carSearch" lay-verify="required">
                        <option value="">请选择车辆</option>
                    </select>
                </div>
                <div class="layui-input-inline">
                    <select id="allMaintain"  lay-verify="required" name="maintains.mid">
                        <option value="">请选择保养类型</option>
                    </select>
                </div>
                <a class="layui-btn search_btn" data-type="reload"  id="searchfees">搜索</a>
            </div>
            <shiro:hasPermission name="maintain:add">
                <div class="layui-inline">
                    <a class="layui-btn layui-btn-normal addMaintain_btn" >+录入保养记录</a>
                </div>
            </shiro:hasPermission>
            <shiro:hasPermission name="maintain:deleteBatch">
                <div class="layui-inline">
                    <a class="layui-btn layui-btn-danger layui-btn-normal delAll_btn">批量删除</a>
                </div>
            </shiro:hasPermission>
        </form>
    </blockquote>
    <table id="mainRecordList" lay-filter="mainRecordList"></table>
    <!--操作-->
    <script type="text/html" id="feesListBar">
        <shiro:hasPermission name="maintain:edit">
            <a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>
        </shiro:hasPermission>
        <shiro:hasPermission name="maintain:del">
            <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
        </shiro:hasPermission>

    </script>
</form>
<script type="text/javascript" src="/static/layui/layui/layui.js"></script>
<script>
    layui.use(['form','layer','laydate','table','laytpl'],function(){
        var form = layui.form,
            layer = parent.layer === undefined ? layui.layer : top.layer,
            $ = layui.jquery,
            laytpl = layui.laytpl,
            table = layui.table,
            laydate=layui.laydate;

        //用户列表
        var tableIns = table.render({
            elem: '#mainRecordList',
            url : '/maintainRecord/maintainRecords',
            cellMinWidth : 95,
            page : true,
            height : "full-125",
            limits : [5,10,15,20,25],
            limit : 5,
            id : "mainRecordListTable",
            cols : [[
                {type: "checkbox", fixed:"left", width:50},
                {field: 'carcaplate', title: '车牌号码', minWidth:100, align:"center",templet:function(d){
                    return d.cars.carplate
                }},
                {field: 'mtype', title: '项目名称', minWidth:100, align:"center",templet:function (d) {
                    return d.maintains.mtype
                }},
                {field: 'btypename', title: '汽车销售维修站', align:'center',templet:function (d) {
                    return d.btypes.btypename
                }},
                {field: 'maintainkm', title: '保养里程',  align:'center'},
                {field: 'next_maintainkm', title: '下次保养里程',  align:'center'},
                {field: 'maintainprice', title: '保养费用',  align:'center'},
                {field: 'maintaintime', title: '保养时间',  align:'center'},
                {field: 'next_maintaintime', title: '下次保养时间',  align:'center'},
                {title: '操作', minWidth:175, templet:'#feesListBar',fixed:"right",align:"center"}
            ]]

        });
        //获得保养项目
        $.ajax({
            url:"/maintain/allMaintain",
            success:function (res) {
                for(var i=0;i<res.data.length;i++){
                    var op=$("<option></option>");
                    op.attr('value',res.data[i].mid);
                    op.append(res.data[i].mtype);
                    $("#allMaintain").append(op);
                }
                form.render('select');
            }
        })

        //获取car数据
        $.ajax({
            url:"/car/allcar",
            success:function (res) {
                for(var i=0;i<res.data.length;i++){
                    var op=$("<option></option>");
                    op.attr('value',res.data[i].carid);
                    op.append(res.data[i].carplate);
                    $("#carSearch").append(op);
                }
                form.render('select');
            }
        });

        //搜索
        $("#searchfees").click(function () {
            var mid=$("#allMaintain").val();
            var carid=$('#carSearch').val();
            tableIns.reload({
                where:{
                    mid:mid,
                    carid:carid
                }
            })
        })

        $(".addMaintain_btn").click(function () {
            var addUser= layui.layer.open({
                title:"录入保养信息",
                type: 2,
                content: '/page/RoutineWork/maintainadd.jsp',
                shade:0.3,
                anim:0,
                area:["70%","90%"],
                maxmin:true,
                success:function () {
                    setTimeout(function(){
                        layui.layer.tips('点击此处返回保养记录列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    },1500)
                }
            });
////           layui.layer.full(addUser);
        })
        //c操作
        table.on('tool(mainRecordList)', function(obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            switch (layEvent){
                case "del":
                    layer.confirm('是否要删除', function(index){
                        delFeesRecord(data.maintainid);
                        layer.close(index);
                    });
                    break;
                case "edit":
                    edit(data);
                    break;
            }
        });
        //修改
        function edit(data) {
            var update= layui.layer.open({
                title:"修改保养记录",
                type: 2,
                content: '/page/RoutineWork/maintainUpdate.jsp',
                shade:0.3,
                anim:0,
                area:["70%","90%"],
                maxmin:true,
                success:function (layero, index) {
                    //少了这个就不能从父页面向子页面传值
                    var body = layui.layer.getChildFrame('body', index);
                    body.contents().find("#tempCarid").val(data.cars.carid);
                    body.contents().find("#tempBtypeid").val(data.btypes.btypeid);
                    body.contents().find("#tempID").val(data.maintainid);
                    body.contents().find("#tempMid").val(data.maintains.mid);
                    body.contents().find("#maintaintime").val(data.maintaintime);
                    body.contents().find("#maintainprice").val(data.maintainprice);
                    body.contents().find("#maintainkm").val(data.maintainkm);
                    body.contents().find("#remark").val(data.remark)

                    setTimeout(function(){
                        layui.layer.tips('点击此处返回规费列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    },1500)
                }
            });
        }
        //删除
        function delFeesRecord(maintainid) {
            $.ajax({
                type:"get",
                url:"/maintainRecord/deleteMaintainRecord",
                data:{maintainid:maintainid},
                success:function (de) {
                    console.log(de);
                    if(de.code==200){
                        layui.layer.msg(de.msg)
                        tableIns.reload({
                        });
                    }
                }
            });
        }

        $(".delAll_btn").click(function () {
            var checkStatus=table.checkStatus('mainRecordListTable');
            var data=checkStatus.data;
            var idArray=new Array();
            if(data.length>0){
                for(var i=0;i<data.length;i++){
                    idArray.push(data[i].maintainid);
                }
                $.ajax({
                    url:'/maintainRecord/deleteBatchMaintain',
                    type:'GET',
                    data:{maintainIds:idArray},
                    dataType:"JSON",
                    success:function (res) {
                        if(res.code==200){
                            //提示
                            layui.layer.msg(res.msg);
                            //刷新表格
                            tableIns.reload();
                        }
                    }
                });
            }else{
                layui.layer.msg("你至少要选择一行");
            }
        });
    })

</script>
</body>
</html>